package com.dahuyou.netty.jmx;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    private MetricsHandler metricsHandler = new MetricsHandler();
    @Override
    protected void initChannel(SocketChannel channel) {

        // 解码转String，注意调整自己的编码格式GBK、UTF-8 byte->string
        channel.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));

        // 增加内置编码器，这样向对端发送消息就不要转换为二进制数据了
        channel.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
        //在管道中添加我们自己的接收数据实现方法
//        channel.pipeline().addLast(new MyServerHandler());
        // metric handler计算实时连接数
//        channel.pipeline().addLast(new MetricsHandler());
        channel.pipeline().addLast(metricsHandler);

    }

}
